package com.piggy.common.core.web.page;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.piggy.common.core.utils.PageUtils;
import com.piggy.common.core.utils.ServletUtils;

/**
 * 表格数据处理
 *
 * @author shark
 */
public class TableSupport {
    /**
     * 当前记录起始索引
     */
    public static final String PAGE_NUM = "pageNum";

    /**
     * 每页显示记录数
     */
    public static final String PAGE_SIZE = "pageSize";

    /**
     * 排序列
     */
    public static final String ORDER_BY_COLUMN = "orderByColumn";

    /**
     * 创建时间列
     */
    public static final String CREATE_TIME_COLUMN = "create_time";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    public static final String IS_ASC = "isAsc";

    /**
     * 封装分页对象
     */
    public static PageDomain getPageDomain() {
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(ServletUtils.getParameterToInt(PAGE_NUM));
        pageDomain.setPageSize(ServletUtils.getParameterToInt(PAGE_SIZE));
        String isAsc = ServletUtils.getParameter(IS_ASC);
        if (StrUtil.isNotBlank(isAsc) && isAsc.equalsIgnoreCase(OrderByItem.C_DESC)) {
            pageDomain.desc(ServletUtils.getParameter(ORDER_BY_COLUMN));
        } else {
            pageDomain.asc(ServletUtils.getParameter(ORDER_BY_COLUMN));
        }
        return pageDomain;
    }

    /**
     * 封装分页对象
     */
    public static PageDomain getPageDomain(Boolean isDefault) {
        PageDomain pageDomain = getPageDomain();
        if (ObjectUtil.isNotEmpty(isDefault) && isDefault) {
            pageDomain.desc(CREATE_TIME_COLUMN);
        }
        return pageDomain;
    }

    /**
     * 封装分页对象
     */
    public static PageDomain getPageDomain(Object bo) {
        Integer pageNum = ObjectUtil.defaultIfNull((Integer) BeanUtil.getFieldValue(bo, PAGE_NUM), PageUtils.DEFAULT_PAGE_NUM);
        Integer pageSize = ObjectUtil.defaultIfNull((Integer) BeanUtil.getFieldValue(bo, PAGE_SIZE), PageUtils.DEFAULT_PAGE_SIZE);
        String orderByColumn = (String) BeanUtil.getFieldValue(bo, ORDER_BY_COLUMN);
        String isAsc = (String) BeanUtil.getFieldValue(bo, IS_ASC);
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(pageNum);
        pageDomain.setPageSize(pageSize);
        if (StrUtil.isNotBlank(isAsc) && isAsc.equalsIgnoreCase(OrderByItem.C_DESC)) {
            pageDomain.desc(orderByColumn);
        } else {
            pageDomain.asc(orderByColumn);
        }
        return pageDomain;
    }

    /**
     * 封装分页对象
     */
    public static PageDomain getPageDomain(Object bo, Boolean isDefault) {
        PageDomain pageDomain = getPageDomain(bo);
        if (ObjectUtil.isNotEmpty(isDefault) && isDefault) {
            pageDomain.desc(CREATE_TIME_COLUMN);
        }
        return pageDomain;
    }

    public static PageDomain buildPageRequest() {
        return getPageDomain();
    }

    public static PageDomain buildPageRequest(Boolean isDefault) {
        return getPageDomain(isDefault);
    }

    public static PageDomain buildPageRequest(Object bo) {
        return getPageDomain(bo);
    }

    public static PageDomain buildPageRequest(Object bo, Boolean isDefault) {
        return getPageDomain(bo, isDefault);
    }

}
